{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from FourierTransform import *\n",
    "from gauseKernel import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def downSampling(filepath, P):\n",
    "    imageRGB = Image.open(filepath)\n",
    "    imageL = imageRGB.convert(\"L\")\n",
    "    imageL_arr = np.array(imageL)\n",
    "    print(imageL_arr.shape)\n",
    "    conv = Conv2D(1, 3, 3, \"SAME\")\n",
    "    imageL_arr = conv(imageL_arr)[0]\n",
    "    FT = FourierTransform()\n",
    "    B, F, Fabs, e = FT(imageL_arr)\n",
    "    Bcontainer = {}\n",
    "    for key, value in B.items():\n",
    "        tmpB = []\n",
    "        for i in range(0, len(value), P):\n",
    "            tmpVector = []\n",
    "            for j in range(0, len(value[0]), P):\n",
    "                tmpVector.append(value[i][j])\n",
    "            tmpB.append(tmpVector)\n",
    "        Bcontainer[key] = np.array(tmpB)\n",
    "    return Bcontainer, F\n",
    "\n",
    "def reconImage(B, F):\n",
    "    h = len(B[\"0,0\"])\n",
    "    w = len(B[\"0,0\"][0])\n",
    "    print(h,w)\n",
    "    tmpList = [[0.0 + 0.0j for i in range(w)] for j in range(h)]\n",
    "    recon = np.array(tmpList)\n",
    "    for i in range(h):\n",
    "        for j in range(w):\n",
    "            i_ = i - h // 2\n",
    "            j_ = j - w // 2\n",
    "            recon = recon + F[i][j] * F.shape[0] * F.shape[1] / (h * w) * B[str(i_) + \",\" + str(j_)]\n",
    "    reconImage = Image.fromarray(recon.astype(\"uint\")).convert(\"L\")\n",
    "    reconImage.save(\"./reconImage.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(95, 100)\n",
      "-47\n",
      "-46\n",
      "-45\n",
      "-44\n",
      "-43\n",
      "-42\n",
      "-41\n",
      "-40\n",
      "-39\n",
      "-38\n",
      "-37\n",
      "-36\n",
      "-35\n",
      "-34\n",
      "-33\n",
      "-32\n",
      "-31\n",
      "-30\n",
      "-29\n",
      "-28\n",
      "-27\n",
      "-26\n",
      "-25\n",
      "-24\n",
      "-23\n",
      "-22\n",
      "-21\n",
      "-20\n",
      "-19\n",
      "-18\n",
      "-17\n",
      "-16\n",
      "-15\n",
      "-14\n",
      "-13\n",
      "-12\n",
      "-11\n",
      "-10\n",
      "-9\n",
      "-8\n",
      "-7\n",
      "-6\n",
      "-5\n",
      "-4\n",
      "-3\n",
      "-2\n",
      "-1\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n",
      "20\n",
      "21\n",
      "22\n",
      "23\n",
      "24\n",
      "25\n",
      "26\n",
      "27\n",
      "28\n",
      "29\n",
      "30\n",
      "31\n",
      "32\n",
      "33\n",
      "34\n",
      "35\n",
      "36\n",
      "37\n",
      "38\n",
      "39\n",
      "40\n",
      "41\n",
      "42\n",
      "43\n",
      "44\n",
      "45\n",
      "46\n",
      "47\n"
     ]
    }
   ],
   "source": [
    "filepath = \"1.jpg\"\n",
    "B, F = downSampling(filepath, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "48 50\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\inspur\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\ipykernel_launcher.py:32: ComplexWarning: Casting complex values to real discards the imaginary part\n"
     ]
    }
   ],
   "source": [
    "reconImage(B, F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
